package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category, HttpSession session) {
        Long id = (Long) session.getAttribute("employee");
        category.setCreateUser(id);
        category.setUpdateUser(id);
        categoryService.save(category);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<Category>> findByPage(Integer page,Integer pageSize){
        Page<Category> pageData = categoryService.findByPage(page, pageSize);
        return R.success(pageData);
    }

    @DeleteMapping
    public R<String> deleteById(Long id) {
        R<String> r = categoryService.deleteById(id);
        return r;
    }

    @PutMapping
    public R<String> update(@RequestBody Category category,HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        category.setUpdateUser(id);
        categoryService.update(category);
        return R.success("修改成功");
    }


@RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {

    response.setHeader("content-disposition","attachment;filename=category.xlsx");

    //1. 读取模板的输入流
    InputStream inputStream = this.getClass().getResourceAsStream("/excel/category.xlsx");
    //2。 使用模板的输入流构建一个工作薄
    Workbook workbook = new XSSFWorkbook(inputStream);
    //3. 得到工作单
    Sheet sheet = workbook.getSheetAt(0);
    //4. 读取第一行,提取第一行的样式，并且存储到集合中
    Row row = sheet.getRow(1);
    List<CellStyle> cellStylesList= new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        //得到单元格的样式
        CellStyle cellStyle = row.getCell(i).getCellStyle();
        cellStylesList.add(cellStyle);
    }
    //5. 得到数据,遍历数据，把数据写入到excel
    List<Category> categoryList = categoryService.findAll(null);
    for (int i = 0; i < categoryList.size(); i++) {
        //一个类别就对应一行
        row =  sheet.createRow(i+1);
        Category category = categoryList.get(i);
        //把类别信息设置到单元格上
        //类别类型
        Cell cell = row.createCell(0);
        //设置单元格内容
        cell.setCellValue(category.getType());
        cell.setCellStyle(cellStylesList.get(0));
        //类别名字
        cell = row.createCell(1);
        //设置单元格内容
        cell.setCellValue(category.getName());
        cell.setCellStyle(cellStylesList.get(1));
        //类别排序
        cell = row.createCell(2);
        //设置单元格内容
        cell.setCellValue(category.getSort());
        cell.setCellStyle(cellStylesList.get(2));
        //创建时间
        cell = row.createCell(3);
        //设置单元格内容
        cell.setCellValue(category.getCreateTime().toLocalDate().toString());
        cell.setCellStyle(cellStylesList.get(3));
        //创建时间
        cell = row.createCell(4);
        //设置单元格内容
        cell.setCellValue(category.getUpdateTime().toLocalDate().toString());
        cell.setCellStyle(cellStylesList.get(4));
    }
        //6.把工作薄写出
    workbook.write(response.getOutputStream());
    }


    @RequestMapping("/list")
    public R<List<Category>> list(Integer type){
        List<Category> categoryList = categoryService.findAllByType(type);
        return R.success(categoryList);
    }



}